package cn.com.cloudpioneer.evaluate.service;

import cn.com.cloudpioneer.evaluate.mapper.ItemMapper;
import cn.com.cloudpioneer.evaluate.mapper.TaskMapper;
import cn.com.cloudpioneer.evaluate.mapper.UserMapper;
import cn.com.cloudpioneer.evaluate.model.ItemModel;
import cn.com.cloudpioneer.evaluate.model.ResourceModel;
import cn.com.cloudpioneer.evaluate.model.TaskModel;
import cn.com.cloudpioneer.evaluate.model.UserModel;
import cn.com.cloudpioneer.evaluate.utils.CustomException;
import cn.com.cloudpioneer.evaluate.utils.Generate;
import cn.com.cloudpioneer.evaluate.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @auther maruikai
 * @create 2023/9/13-14:35
 */
@Service
public class ItemService {

    @Resource
    private ItemMapper itemMapper;
    @Resource
    private TaskMapper taskMapper;
    @Resource
    private UserMapper userMapper;

    @Autowired
    private TaskService taskService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private FileService fileService;

    @Transactional(rollbackFor = Exception.class)
    public Object setWork(ItemModel workModel) throws CustomException {
        int flag;
        StringBuilder userIds = new StringBuilder();
        if (workModel.getAppraiserList().size() > 0){
            for (UserModel userModel : workModel.getAppraiserList()) {
                userIds.append(userModel.getUserId()).append(",");
            }
        }
        workModel.setAppraiser(userIds.toString());

        if (StringUtil.isEmpty(workModel.getItemId())) {
            workModel.setItemId(Generate.generateId());
            flag = itemMapper.addWork(workModel);
            if (flag < 0) throw new CustomException(5001, "操作失败");
            return flag;
        }
        flag = itemMapper.updateWork(workModel);
        if (flag < 0) throw new CustomException(5001, "操作失败");
        return flag;
    }

    @Transactional(rollbackFor = Exception.class)
    public Object delWork(ItemModel workModel) throws CustomException {
        if (StringUtil.isEmpty(workModel.getItemId())) {
            throw new CustomException(5001, "操作失败");
        }
        return itemMapper.delWork(workModel.getItemId());
    }

    @Transactional(rollbackFor = Exception.class)
    public Object achieveWork(ItemModel workModel) throws CustomException {
        if (StringUtil.isEmpty(workModel.getItemId())) {
            throw new CustomException(5001, "操作失败");
        }
        return itemMapper.updateWork(workModel);
    }

    public List<ResourceModel> getReportFileList(String itemId) throws CustomException {
        ItemModel byId = itemMapper.findById(itemId);
        if (byId == null) throw new CustomException(5005, "无法找到此项目!");

        List<ResourceModel> resList = resourceService.findByObjId(itemId);
        for (ResourceModel resourceModel : resList) {
            if (!StringUtil.isEmpty(resourceModel.getFilePath())) {
                String viewUrl = fileService.formatViewUrl(resourceModel.getFilePath());
                resourceModel.setPreviewUrl(viewUrl);
            }
        }
        return resList;
    }


    public Object getItemInfo(String itemId) throws CustomException {
        ItemModel byId = itemMapper.findById(itemId);
        if (byId == null) {
            throw new CustomException(5001, "操作失败");
        }
//        ResItemVo resItemVo = new ResItemVo();
//        BeanUtils.copyProperties(byId, resItemVo);
        if (!StringUtil.isEmpty(byId.getFilePath())) {
            String fileName = byId.getFilePath().substring(byId.getFilePath().lastIndexOf("/") + 1);
            byId.setFileName(fileName);
        }
        List<TaskModel> taskList = taskService.getTaskList(itemId);
        if (taskList.size() > 0) {
            int scour = 0;
            for (TaskModel taskModel : taskList) {
                scour += taskModel.getScore();
            }
            byId.setScore(scour / taskList.size());
        }
        byId.setTaskModel(taskList);
        List<String> userId = new ArrayList<>(Arrays.asList(byId.getAppraiser().split(",")));
        List<UserModel> byIds = userMapper.findByIds(userId);
        byId.setAppraiserList(byIds);
        return byId;
    }

    public Object getWorkList(String keyword, String userId, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<ItemModel> achieveList = itemMapper.findAchieveList(keyword, userId, false);
        for (ItemModel resItemVo : achieveList) {
            List<String> userIds = new ArrayList<>(Arrays.asList(resItemVo.getAppraiser().split(",")));
            List<UserModel> byIds = userMapper.findByIds(userIds);
            resItemVo.setAppraiserList(byIds);
        }
        return new PageInfo<>(achieveList);
    }

    public Object getAchieveList(String keyword, String userId, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<ItemModel> achieveList = itemMapper.findAchieveList(keyword, userId, true);
        for (ItemModel itemModel : achieveList) {
            int score = 0;
            List<TaskModel> byItemIn = taskMapper.findParentByItemId(itemModel.getItemId());
            if (byItemIn.size() > 0) {
                for (TaskModel taskModel : byItemIn) {
                    score += taskModel.getScore();
                }
                if (score != 0) {
                    itemModel.setScore(score / byItemIn.size());
                }
            }
            List<String> userIds = new ArrayList<>(Arrays.asList(itemModel.getAppraiser().split(",")));
            List<UserModel> byIds = userMapper.findByIds(userIds);
            itemModel.setAppraiserList(byIds);
        }
        return new PageInfo<>(achieveList);
    }

}
